﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace JieBooLaw.WebAdmin
{
    using JieBooLaw.Service;
    using JieBooLaw.Domain;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Summary description for ValidationService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class ValidationService : System.Web.Services.WebService
    {

        [WebMethod]
        public object IsHasAccount(string id, string value)
        {
            TSys_UserSvc svc = new TSys_UserSvc();
            IList<TSys_User> user = svc.GetByEntity(new TSys_User { Account = value });
            bool success = user.Count > 0;
            System.Threading.Thread.Sleep(1000);
            return new { valid = success, message = success ? "" : "用户名已存在！" };
        }

        [WebMethod]
        public object IsMobile(string id, string value)
        {
            Match m = new Regex("^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "请输入正确的手机号码！" };
        }

        [WebMethod]
        public object IsEmail(string id, string value)
        {
            Match m = new Regex("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "请输入正确的邮箱格式！" };
        }

        [WebMethod]
        public object IsHasCHZN(string id, string value)
        {
            Match m = new Regex("[\u4e00-\u9fa5]").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "存在中文字符！" };
        }

        [WebMethod]
        public object IsNumber(string id, string value)
        {
            Match m = new Regex("^[0-9]+$").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "请输入有效的数字！" };
        }

        [WebMethod]
        public object IsNumberSign(string id, string value)
        {
            Match m = new Regex("^[+-]?[0-9]+$").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "请输入有效的数字（可带正负号）！" };
        }

        [WebMethod]
        public object IsDecimal(string id, string value)
        {
            decimal temp = 0;
            bool success = Decimal.TryParse(value, out temp);
            System.Threading.Thread.Sleep(1000);
            return new { valid = success, message = success ? "" : "请输入有效的浮点数！" };
        }

        [WebMethod]
        public object IsDecimalSign(string id, string value)
        {
            Match m = new Regex("^[+-]?[0-9]+[.]?[0-9]+?$").Match(value);
            System.Threading.Thread.Sleep(1000);
            return new { valid = m.Success, message = m.Success ? "" : "请输入有效的浮点数（可带正负号）！" };
        }
    }
}
